package by.euanpa.android.grodnobusstop.loader;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;
import by.euanpa.android.grodnobusstop.bo.BusStop;
import by.euanpa.android.grodnobusstop.bo.Route;
import by.euanpa.android.grodnobusstop.content.provider.contract.Contract.BusStopColumns;
import by.euanpa.android.grodnobusstop.content.provider.contract.Contract.RelationColumns;
import by.euanpa.android.grodnobusstop.content.provider.contract.Contract.RouteColumns;
import by.euanpa.android.grodnobusstop.utils.CursorUtils;

public abstract class BusStopLoader extends AsyncTaskLoader<List<BusStop>> {

	private static final String EMPTY = "";
	private List<BusStop> busStops;
	private UpdateDbReceiver dbObserver;
	
	public BusStopLoader(Context context) {
		super(context);
		Log.d("neron", "init");
	}

	@Override
	public List<BusStop> loadInBackground() {
		List<BusStop> newBusStops = new ArrayList<BusStop>();
		String busStopSelection = getBusStopSelection();
		Cursor busStopCursor = getContext().getContentResolver().query(BusStopColumns.getUri(), null, busStopSelection, null, null);
		while (busStopCursor.moveToNext()) {
			BusStop busStop = new BusStop();
			busStop.setName(CursorUtils.getString(BusStopColumns.NAME, busStopCursor));
			busStop.setLongitude(CursorUtils.getInt(BusStopColumns.NAME, busStopCursor));
			busStop.setLatitude(CursorUtils.getInt(BusStopColumns.NAME, busStopCursor));
			Cursor relationCursor = getContext().getContentResolver().query(RelationColumns.getUri(), null, RelationColumns.BUS_STOP_ID + " = ? ",
					new String[] { CursorUtils.getInt(BusStopColumns.ID, busStopCursor) + EMPTY }, null);
			List<Route> routes = new ArrayList<Route>();
			while (relationCursor.moveToNext()) {
				Cursor routeCursor = getContext().getContentResolver().query(RouteColumns.getUri(), null, RouteColumns.ID + " = ? ",
						new String[] { CursorUtils.getInt(RelationColumns.ROUTE_ID, relationCursor) + EMPTY }, null);
				routeCursor.moveToFirst();
				Route route = new Route();
				route.setName(CursorUtils.getString(RouteColumns.NAME, routeCursor));
				route.setNumber(CursorUtils.getInt(RouteColumns.NUMBER, routeCursor));
//				route.setTimeTableMonday(TimeTableUtils.toTimeTable(CursorUtils.getString(RelationColumns.TIME_TABLE_MONDAY, relationCursor)));
//				route.setTimeTableSaturday(TimeTableUtils.toTimeTable(CursorUtils.getString(RelationColumns.TIME_TABLE_SATURDAY, relationCursor)));
//				route.setTimeTableWorkday(TimeTableUtils.toTimeTable(CursorUtils.getString(RelationColumns.TIME_TABLE_WORKDAY, relationCursor)));
				routes.add(route);
				CursorUtils.close(routeCursor);
			}
			CursorUtils.close(relationCursor);
			busStop.setRoutes(routes);
			newBusStops.add(busStop);
		}
		CursorUtils.close(busStopCursor);
		for (BusStop busStop : newBusStops) {
			Log.d("neron", busStop.getName() + " name, " + busStop.getRoutes().size() + " routes");
			for (Route route : busStop.getRoutes()) {
				Log.d("neron", route.getName() + " name, " + route.getNumber() + " number");
			}
		}
		return newBusStops;
	}

	@Override
	protected void onStartLoading() {
		Log.d("neron", "start");
		if(busStops != null){
			deliverResult(busStops);
		}
		Log.d("neron", "afterDeliver");
		if(dbObserver == null){
			dbObserver = new UpdateDbReceiver(this);
		}
		getContext().registerReceiver(dbObserver, dbObserver.getFilter());
		super.onStartLoading();
		if(takeContentChanged() || busStops == null){
			forceLoad();
		}
	}
	
	@Override
	protected void onStopLoading() {
		super.onStopLoading();
		cancelLoad();
	}
	
	@Override
	public void deliverResult(List<BusStop> data) {
		Log.d("neron", "deliver");
		if (isReset()) {
			data = null;
		}
		busStops = data;
		if (isStarted()) {
			super.deliverResult(busStops);
		}
	}

	@Override
	public void onCanceled(List<BusStop> data) {
		super.onCanceled(data);
		data = null;
	}

	@Override
	protected void onReset() {
		super.onReset();
		onStopLoading();
		busStops = null;
		if(dbObserver != null){
			getContext().unregisterReceiver(dbObserver);
		}
	}

	protected abstract String getBusStopSelection();

}
